//获取应用实例
const app = getApp();
var util = require('../../../utils/util.js');
var _this;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 赛选
    selectProductFilter: false,
    selectProductFilterBox: false, // 赛选弹窗是否弹出
    selectProductBox: null,
    selectProductData: null,//采集商商品
    productCollectorsList: null,
    brandCollectorsList: null,
    outerIndexProduct: null,
    indexProduct: null,
    recommendProductsList: null,
    buttonClicked: false,
    categoryId: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    _this = this
    // 获取推荐配件
    // 获取参数
    var aa = options;
    console.log('aa',aa);
    // 返回上一个页面
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];  //上一个页面
    _this.setData({
      outerIndexProduct: aa.outerIndex,
      indexProduct: aa.index,
      recommendProductsList: prevPage.data.recommendProductsList
    })
    _this.selectProduct(aa.categoryId, aa.outerIndex, aa.index, aa.parentCategoryId);
  },
  /*  采集商UserCode 添加替换产品 筛选 */
  selectProduct(categoryId, outerIndex, index, parentCategoryId) {
    var adcode = app.globalData.Maintenance.userArea.adcode;
    app.getRecommendedCollectors(adcode, function (res) {
      if (res) {
        var productCollectors = res;
        var collectorsUserCode = res.userCode; // 采集商UserCode
        // 获取赛选品牌类型参数
        var productCollectorsList = res.merchantBrandList;
        for (var i = 0; i < productCollectorsList.length; i++) {
          productCollectorsList[i].checked = false;
          for (var a = 0; a < productCollectorsList[i].brands.length; a++) {
            productCollectorsList[i].brands[a].checked = false;
          }
        }
        var brandCollectorsList = productCollectorsList[0].brands;
        productCollectorsList[0].checked = true;
        brandCollectorsList[0].checked = true;
        
        var re = /^[0-9]+.?[0-9]*$/; // 是否為數字正則
        if (re.test(outerIndex) && re.test(index)) {
          var outerIndexProduct = outerIndex;
          var indexProduct = index;
          _this.setData({
            outerIndexProduct: outerIndex,
            indexProduct: index
          })
        }
        _this.setData({
          productCollectors: productCollectors,
          collectorsUserCode: collectorsUserCode,
          productCollectorsList: productCollectorsList,
          brandCollectorsList: brandCollectorsList
        })
        console.log('brandCollectorsList', _this.data.brandCollectorsList)
        // 获取 采集商商品
        var data={};
        data.merchantCode = collectorsUserCode;
        data.categoryId = categoryId;
        // data.parentCategoryId = parentCategoryId;
        // data.productBrandId = app.globalData.xieYouUserInfoRoot.userBrandModel.brand.id;
        data.brandModelId = app.globalData.xieYouUserInfoRoot.userBrandModel.brandModel.id;
        _this.selectProductList(data);
        // 返回的时候让outerIndexProduct  indexProduct 为null
      } else {
        //P.alertMsg("错误", rs.message);
      }
    })
  },
  /*  获取 采集商商品 */
  selectProductList: function (data) {
    app.getRecommendedCollectorsList(data, function (res) {
      if (res) {
        if (res.status == 'success') {
          _this.setData({
            selectProductBox: res.data,
            selectProductData: res.data.results
          })
          console.log('selectProductData=', _this.data.selectProductData);
        } else {
          //P.alertMsg("错误", rs.message);
          _this.setData({
            outerIndexProduct: null,
            indexProduct: null
          })
        }
      } else {
        //P.alertMsg("错误", "网络异常,请稍后再试");
        _this.setData({
          outerIndexProduct: null,
          indexProduct: null
        })
      }
    });
  },
  // 开启关闭筛选
  selectProductFilterFun: function () {
    var aa = !_this.data.selectProductFilter;
    _this.setData({
      selectProductFilter: aa
    })
  },
  //切换类型
  brandCollectorsListFun:function(e){
    // 获取参数 —— index
    var index = e.target.dataset.index || e.target.dataset.index == 0 ? e.target.dataset.index : e.currentTarget.dataset.index;
    
    var productCollectorsList = _this.data.productCollectorsList;
    for (var i = 0; i < productCollectorsList.length; i++) {
      productCollectorsList[i].checked = false;
    }
    productCollectorsList[index].checked = true;
    var brandCollectorsList = productCollectorsList[index].brands;
    for (var a = 0; a < brandCollectorsList.length;a++){
      brandCollectorsList[a].checked = false
    }
    brandCollectorsList[0].checked = true;
    _this.setData({
      productCollectorsList: productCollectorsList,
      brandCollectorsList: brandCollectorsList
    })
  },
  // 切换品牌
  brandCollectorsListNavFun:function(e){
    var brandCollectorsList = _this.data.brandCollectorsList;
    var productCollectorsList = _this.data.productCollectorsList;
    var index = e.target.dataset.index || e.target.dataset.index == 0 ? e.target.dataset.index : e.currentTarget.dataset.index;
    for (var a = 0; a < brandCollectorsList.length; a++) {
      brandCollectorsList[a].checked = false
    }
    brandCollectorsList[index].checked = true;
    _this.setData({
      brandCollectorsList: brandCollectorsList
    })
  },
  /*  切换类型，品牌 */
  selectProductTab:function(){
    var test;
    var productCollectorsList = _this.data.productCollectorsList;
    var brandCollectorsList = _this.data.brandCollectorsList;
    for (var i = 0; i < brandCollectorsList.length; i++ ){
      if (brandCollectorsList[i].checked) {
        test = brandCollectorsList[i];
      }
    }
    console.log('test222222222222222222=', test);
    var merchantCode = test.merchantCode;
    var productBrandId = test.productBrand.id;
    _this.selectProductList(merchantCode, null, productBrandId);
    // 关闭切换窗口
    _this.setData({
      selectProductFilter:false
    })
  },
  /*  添加/替换产品 */
  selectProductAdd:function(e){
    // 获取参数 —— index
    var index = e.currentTarget.dataset.index;
    var parentCategoryId = e.currentTarget.dataset.parentCategoryId;
    //var outerIndexProduct,indexProduct;
    var outerIndexProduct = _this.data.outerIndexProduct;
    var indexProduct = _this.data.indexProduct;
    var re = /^[0-9]+.?[0-9]*$/;
    if (re.test(outerIndexProduct) && re.test(indexProduct)) {
      // 首先删除点击替换的产品
      _this.dataChangeDelete(outerIndexProduct, indexProduct, index)
      // 加入产品
      // _this.addSelectProductFun(parentCategoryId,index);
      // wx.navigateTo({
      //   url: '../recommend-products-list?getModel=true'
      // })
    } else {
      // 加入产品
      _this.addSelectProductFun(parentCategoryId,index);
    }
    // 返回上一个页面
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; 
    prevPage.setData({
      recommendProductsList: _this.data.recommendProductsList,
    })
    // 添加计算总价2018-6-13
    prevPage.totalPrice();
    if (!_this.data.buttonClicked) {
      util.buttonClicked(_this);
      wx.navigateBack({
        delta: 1
      })
    }
  },
  addSelectProductFun: function (parentCategoryId,outerIndex){
    var addSelectProduct = _this.data.selectProductData[index];
    addSelectProduct.countShopCar = 1;
    var dataBox = _this.data.recommendProductsList;
    for (var i = 0; i < dataBox.length; i++) {
      if (dataBox[i].categoryId == addSelectProduct.categoryId) {
        if (dataBox[i].recommendProductList){
          dataBox[i].recommendProductList.push(addSelectProduct);
        }else{
          dataBox[i].recommendProductList = [];
          dataBox[i].recommendProductList.push(addSelectProduct);
        }
      }
    }
    //console.log('recommendProductList=', dataBox[i].recommendProductList);
    // 返回上一个页面
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];  //上一个页面
    prevPage.setData({
      recommendProductsList: dataBox
    })
    _this.setData({
      recommendProductsList: dataBox
    })
    // 返回推荐商品页
    // $scope.selectProductFilterBox = false;
  },
  // 替换产品
  replaceProductFun: function (productsList,index, outerIndex) {
    var addSelectProduct = _this.data.selectProductData[index];
    addSelectProduct.countShopCar = 1;
    productsList[outerIndex].recommendProductList.push(addSelectProduct);
    //console.log('recommendProductList=', dataBox[i].recommendProductList);
    // 返回上一个页面
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];  //上一个页面
    prevPage.setData({
      recommendProductsList: productsList
    })
    _this.setData({
      recommendProductsList: productsList
    })
    // 返回推荐商品页
    // $scope.selectProductFilterBox = false;
  },
  /*  删除产品 */
  dataChangeDelete: function (outerIndex, indexProduct, index) {
    var productsList = _this.data.recommendProductsList;
    var categoryId = productsList[outerIndex].recommendProductList[0].categoryId;
    _this.setData({
      categoryId: categoryId
    })
    productsList[outerIndex].recommendProductList.splice(indexProduct, 1);
    // 返回上一个页面
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2];  //上一个页面
    prevPage.setData({
      recommendProductsList: productsList
    })
    _this.setData({
      recommendProductsList: productsList
    })
    // 替换产品
    _this.replaceProductFun(productsList, index, outerIndex)
  }
})

